<html xmlns="http://www.w3.org/1999/xhtml" xmlns:c="http://java.sun.com/jstl/core"
      xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" 
      xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:t="http://myfaces.apache.org/tomahawk"
      xmlns:v="http://shale.apache.org/validator">

<f:view >
<f:loadBundle var="text" basename="#{userForm.bundleName}"/>

<head>
	 <!--script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script-->
	<script src="//code.jquery.com/jquery-1.10.2.js"></script>
	<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
	<link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css"/>
    <title>#{text['userProfile.title']}</title>
    <meta name="menu" content="MyProfileMenu"/>
    <meta name="heading" content="#{text['userProfile.heading']}"/>
<script>
 $(function() {
$( "#datepicker" ).datepicker({
changeMonth: true,
changeYear: true
});
});
</script>

</head>

<div class="separator"></div>

<!--p>
User id is
<h:outputText value=" "/>
<h:outputText value="#{userForm.user.id}"/>
</p>
<p>
From is
<h:outputText value=" "/>
<h:outputText value="#{userForm.from}"/>
</p-->

<h:form id="userForm"  onsubmit="return validateUserForm(this)" binding="#{userForm.form}">

<h:inputHidden value="#{userForm.user.id}" id="id"/>
<h:inputHidden value="#{userForm.user.version}" id="version"/>
<h:inputHidden value="#{userForm.user.password}" id="originalPassword"/>

<input type="hidden" name="from" value="#{userForm.from}" />

<c:if test="${userForm.rememberMe}">
    <h:inputHidden value="#{userForm.user.password}" id="password"/>
    <h:inputHidden value="#{userForm.user.confirmPassword}" id="confirmPassword"/>
</c:if>

<c:if test="${empty userForm.user.version}">
    <input type="hidden" name="encryptPass" value="true" />
</c:if>

<h:messages style="color:red;margin:8px;"/>	

<c:set var="addText" value="#{text['button.add']}"/>

<h:panelGroup styleClass="buttonBar right">
	<h:commandButton value="#{text['button.save']}" action="#{userForm.save}" styleClass="button_small"/>
	<c:if test="${param.from == 'list' and userForm.user.id != userForm.loggedInUser.id}">
		<h:commandButton value="#{text['button.delete']}" action="#{userForm.delete}"
		styleClass="button_small" onclick="bCancel=true; return confirmDelete('User')"/></c:if>
	<h:commandButton value="#{text['button.cancel']}" action="#{userForm.cancel}" immediate="true"
		styleClass="button_small" onclick="bCancel=true"/>
</h:panelGroup>

<h:panelGroup styleClass="info">
	<c:choose>
		<c:when test="${param.from == 'list' or empty userForm.user.id}">
			<h:outputText value="#{text['userProfile.admin.message']}"/>
		</c:when>
		<c:otherwise><h:outputText value="#{text['userProfile.message']}"/></c:otherwise>
	</c:choose>
</h:panelGroup>



	<h2>#{text['user.form.header.login.credentials']}</h2>
	<table class="inputFieldsTable">
		<tr>
			<td class="inputLabelColumn"><h:outputLabel for="username" styleClass="desc" value="#{text['user.username']}"/></td>
			<td>
				<c:choose>
				<c:when test="${param.from == 'list' and userForm.user.id != userForm.loggedInUser.id}">
					<h:inputText value="#{userForm.user.username}" id="username" required="true" styleClass="text medium">
						<v:commonsValidator client="true" type="required" arg="#{text['user.username']}"/>
					</h:inputText>
				</c:when>
				<c:otherwise>
					<h:inputHidden value="#{userForm.user.username}" id="username"/>				
					<h:outputText value="#{userForm.user.username}" styleClass="text medium"/>
				</c:otherwise>
				</c:choose>
			</td>
			<td><t:message for="username" styleClass="fieldError"/></td>
		</tr>
		<c:if test="${!userForm.rememberMe}">
			<tr>
				<td><h:outputLabel for="password" value="#{text['user.password']}" styleClass="desc"/></td>
				<td><h:inputSecret value="#{userForm.user.password}" id="password" redisplay="true" required="true" styleClass="text medium">
						<v:commonsValidator client="true" type="required" arg="#{text['user.password']}"/>
					</h:inputSecret></td>
				<td><t:message for="password" styleClass="fieldError"/></td>
			</tr>
			<tr>
				<td><h:outputLabel for="confirmPassword" value="#{text['user.confirmPassword']}" styleClass="desc"/></td>
				<td><h:inputSecret value="#{userForm.user.confirmPassword}" id="confirmPassword" redisplay="true" required="true" styleClass="text medium">
						<v:commonsValidator client="true" type="required" arg="#{text['user.confirmPassword']}"/>
						<t:validateEqual for="password" message="#{text['errors.password.not.match']}"/>
					</h:inputSecret></td>
				<td><t:message for="confirmPassword" styleClass="fieldError"/></td>
			</tr>
		</c:if>
		<!--tr>
			<td><h:outputLabel for="passwordHint" value="#{text['user.passwordHint']}" styleClass="desc"/></td>
			<td><h:inputText value="#{userForm.user.passwordHint}" id="passwordHint" required="true" styleClass="text medium">
					<v:commonsValidator client="true" type="required" arg="#{text['user.passwordHint']}"/>
				</h:inputText></td>
			<td><t:message for="passwordHint" styleClass="fieldError"/></td>
		</tr-->
	</table>
		
		
		
	<h2>#{text['user.form.header.personal.info']}</h2>
	<table class="inputFieldsTable">
		<tr>
			<td class="inputLabelColumn"><h:outputLabel for="firstName" value="#{text['user.firstName']}" styleClass="desc"/></td>
			<td>
				<h:inputText id="firstName" value="#{userForm.user.firstName}" maxlength="50" required="true" styleClass="text medium">
					<v:commonsValidator client="true" type="required" arg="#{text['user.firstName']}"/>
				</h:inputText>			</td>
			<td><t:message for="firstName" styleClass="fieldError"/></td>
		</tr>
		<tr>
			<td><h:outputLabel for="lastName" value="#{text['user.lastName']}" styleClass="desc"/></td>
			<td>
				<h:inputText value="#{userForm.user.lastName}" id="lastName" maxlength="50" required="true" styleClass="text medium">
					<v:commonsValidator client="true" type="required" arg="#{text['user.lastName']}"/>
				</h:inputText></td>
			<td><t:message for="lastName" styleClass="fieldError"/></td>
		</tr>
		<tr>
			<td><h:outputLabel for="email" value="#{text['user.email']}" styleClass="desc"/></td>
			<td>
				<h:inputText value="#{userForm.user.email}" id="email" required="true" styleClass="text medium">
					<f:validator validatorId="org.apache.myfaces.validator.Email"/>
					<v:commonsValidator client="true" type="required" arg="#{text['user.email']}"/>
					<v:commonsValidator client="true" type="email" arg="#{text['user.email']}"/>
				</h:inputText></td>
			<td><t:message for="email" styleClass="fieldError"/></td>
		</tr>
		<tr>
			<td><h:outputLabel for="dateOfBirth" value="#{text['user.dateOfBirth']}" styleClass="desc"/></td>
			<td><input type="text" id="datepicker" name="dateOfBirth" /></td>				
		</tr>
		<tr>
			<td><h:outputLabel for="phoneNumber" value="#{text['user.phoneNumber']}" styleClass="desc"/></td>
			<td><h:inputText value="#{userForm.user.phoneNumber}" id="phoneNumber" styleClass="text medium"/></td>
			<td><t:message for="phoneNumber" styleClass="fieldError"/></td>
		</tr>


	</table>
	<!-- Admin properties -->
	<c:choose>
		<c:when test="${param.from == 'list' or param['editUser:add'] == addText}">
			<div class="hr"></div>
			<!-- Account Settings -->
			<h2>#{text['userProfile.accountSettings']}</h2>
			
			<h:selectBooleanCheckbox value="#{userForm.user.enabled}" id="enabled" styleClass="checkbox"/>
			<h:outputLabel for="enabled" styleClass="choice" value="#{text['user.enabled']}"/>

			<h:selectBooleanCheckbox value="#{userForm.user.accountExpired}" id="accountExpired" styleClass="checkbox"/>
			<h:outputLabel for="accountExpired" styleClass="choice" value="#{text['user.accountExpired']}"/>

			<h:selectBooleanCheckbox value="#{userForm.user.accountLocked}" id="accountLocked" styleClass="checkbox"/>
			<h:outputLabel for="accountLocked" styleClass="choice" value="#{text['user.accountLocked']}"/>

			<h:selectBooleanCheckbox value="#{userForm.user.credentialsExpired}" id="credentialsExpired" styleClass="checkbox"/>
				<h:outputLabel for="credentialsExpired" styleClass="choice" value="#{text['user.credentialsExpired']}"/>

			<!-- Assigned Roles -->
			<h2>#{text['userProfile.assignRoles']}</h2>
			
			<h:selectManyCheckbox value="#{userForm.userRoles}" id="userRoles">
				<f:selectItems value="#{userForm.availableRoles}"/>
			</h:selectManyCheckbox>
		</c:when>
		<c:otherwise>
			<c:if test="${not empty param['userForm:userRoles']}">
				<input type="hidden" name="userForm:userRoles" value="${param['userForm:userRoles']}" />
			</c:if>
			<c:forEach var="role" items="#{userForm.userRoles}" varStatus="status">
			   <input type="hidden" name="userForm:userRoles" value="${role}" />
			</c:forEach>
			<h:inputHidden value="#{userForm.user.enabled}"/>
			<h:inputHidden value="#{userForm.user.accountExpired}"/>
			<h:inputHidden value="#{userForm.user.accountLocked}"/>
			<h:inputHidden value="#{userForm.user.credentialsExpired}"/>
			<!--h:inputHidden value="#{userForm.user.orgs.objectId}"/>
			<h:inputHidden value="#{userForm.user.orgs.organizationName}"/>
			<h:inputHidden value="#{userForm.user.orgs.organizationDescription}"/>
			<h:inputHidden value="#{userForm.user.orgs.organizationName}" id="userOrgId"/-->
			<c:forEach var="org" items="${userForm.user.orgList}" varStatus="status">
			  <h:inputHidden value="#{org.objectId}"/>
			  <h:inputHidden value="#{org.organizationName}"/>
			  <h:inputHidden value="#{org.organizationDescription}"/>
			  <h:inputHidden value="#{org.organizationName}" id="userOrgId"/>
			</c:forEach>
		</c:otherwise>
	</c:choose>

	<c:if test="${not empty userForm.userOrgStr}">
		<!-- Assigned Organization -->
		<h2>Assigned Organization</h2>
		<h:selectOneMenu value="#{userForm.userOrgStr}" id="userOrg" rendered="#{not userForm.editOrg}">
			<f:selectItems value="#{userForm.availableOrgs}"/>
		</h:selectOneMenu>
		<h:outputText value="#{userForm.userOrgStr}" rendered="#{userForm.editOrg}" />
		<!--h:inputHidden value="#{userForm.user.orgs.objectId}"/>
		<h:inputHidden value="#{userForm.user.orgs.organizationName}"/>
		<h:inputHidden value="#{userForm.user.orgs.organizationDescription}"/-->
		<c:forEach var="org" items="${userForm.user.orgList}" varStatus="status">
		  <h:inputHidden value="#{org.objectId}"/>
		  <h:inputHidden value="#{org.organizationName}"/>
		  <h:inputHidden value="#{org.organizationDescription}"/>
		</c:forEach>
	</c:if>
	
	<!-- Buttons -->
	<span class="buttongroup">
		<h:commandButton value="#{text['button.save']}" action="#{userForm.save}" styleClass="button_small"/>
		<c:if test="${param.from == 'list' and userForm.user.id != userForm.loggedInUser.id}">
			<h:commandButton value="#{text['button.delete']}" action="#{userForm.delete}"
			styleClass="button_small" onclick="bCancel=true; return confirmDelete('User')"/>
		</c:if>
		<h:commandButton value="#{text['button.cancel']}" action="#{userForm.cancel}" immediate="true"
			styleClass="button_small" onclick="bCancel=true"/>
		<h:commandButton value="Hidden" onClick="#{userForm.edit}" id="loadUser" immediate="true" style="display:none"/>
	</span>
	
</h:form>

<v:validatorScript functionName="validateUserForm"/>
<script type="text/javascript">
    Form.focusFirstElement($('userForm'));
    highlightFormElements();
    
    function loadUserContext()
    {
    	document.getElementById('userForm:loadUser').click()
      alert("hit");
    }
    
</script>


</f:view>
</html>
